﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Premiere.DTO;
using System.Collections;
using Premiere.Window;
using System.Data;

namespace PremiereMX
{
    public class CurrencyLogic
    {
        CurrencyDTO m_subject = new CurrencyDTO();
        IMainForm m_mainForm;
        IUserControlCurrency m_form;
        EditMode m_editMode = EditMode.OK;
        public CurrencyLogic(IMainForm mainForm, IUserControlCurrency form)
        {
            m_mainForm = mainForm;
            m_form = form;
        }
        public void Init()
        {
            updateForm();
            m_form.SetToReadOnly(true);
            SetButtonEditMode(EditMode.OK);
            updateCurrencyList();
        }
        private void updateForm()
        {
            m_form.Code = m_subject.Code;
            m_form.Named = m_subject.Name;
            m_form.LastUpdate = m_subject.LastUpdate;
            m_form.ComputerName = m_subject.ComputerName;
            m_form.UserUpdateBy = m_subject.UserName;
            m_form.AutoAdjustment = m_subject.AutoAdjustment;
            m_form.DecimalPlace = m_subject.DecimalPlace;
            m_form.RoundingType = m_subject.RoundingType;
            m_form.Symbol = m_subject.Symbol;
            m_form.IsDefault = m_subject.IsDefault;
        }
        private void SetButtonEditMode(EditMode editMode)
        {
            m_form.SetButtonEditMode(editMode);
            m_editMode = editMode;
        }
        private void updateCurrencyList()
        {
            IList list = ServiceRegistry.GetInstance().GetCurrencyService().LoadAll();
            m_form.UpdateList(list);
        }
        public void Edit()
        {
            m_form.SetToReadOnly(false);
            m_form.SetButtonEditMode(EditMode.Insert);
        }
        private void updateSubject()
        {
            m_subject.Code = m_form.Code;
            m_subject.ComputerName = Environment.MachineName;
            m_subject.Name = m_form.Named;
            m_subject.Note = string.Empty;
            m_subject.UserName = m_mainForm.GetUser().Code;
            m_subject.AutoAdjustment = m_form.AutoAdjustment;
            m_subject.DecimalPlace = m_form.DecimalPlace;
            m_subject.IsDefault = m_form.IsDefault;
            m_subject.LastUpdate = DateTime.Now;
            m_subject.RoundingType = m_form.RoundingType;
            m_subject.Symbol = m_form.Symbol;
        }
        internal void UpdateSubject(CurrencyDTO curr)
        {
            m_subject = curr;
            m_form.ClearForm();
            updateForm();
            m_form.SetToReadOnly(true);
            SetButtonEditMode(EditMode.OK);
        }
        public void Save()
        {
            try
            {
                if (m_form.IsValidSubject())
                {
                    updateSubject();
                    m_subject.Id = ServiceRegistry.GetInstance().GetCurrencyService().Save(m_subject);
                    m_form.ShowMessage("Record has been saved!");
                    m_form.SetToReadOnly(true);
                    SetButtonEditMode(EditMode.OK);
                    m_form.UpdateList(new object[] { m_subject });
                }
            }
            catch (Exception ex)
            {
                m_form.ShowMessage(ex.Message);
            }
        }
        internal void Delete()
        {
            if (m_subject.Id == 0) return;
            ServiceRegistry.GetInstance().GetCurrencyService().Delete(m_subject);
            m_form.RemoveItem(m_subject.Id);
            m_form.ClearForm();
        }
        internal void New()
        {
            m_subject = new CurrencyDTO();
            m_form.ClearForm();
            SetButtonEditMode(EditMode.Insert);
            m_form.SetToReadOnly(false);
            m_form.FocusToCode();
        }
        public void Refresh()
        {
        }
        internal void Print()
        {
            DataSet ds = ServiceRegistry.GetInstance().GetCurrencyService().GetPreviewReportData(m_subject.Id);
            m_mainForm.Print(ds);
        }
    }
}
